package com.prj.ufdm.cloud.ms.core;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.prj.ufdm.cloud.base.util.BaseStringUtil;
import com.prj.ufdm.cloud.ms.core.util.CoreRequestHttpUtil;
/**
 * 文件上传测试
 * Description: 
 * @author   
 * @date 2019年10月23日
 */
public class UploadFileTest {
	
	public static String host = "http://192.168.70.178:5204";
	// host = "http://115.233.221.131:25202"
	public static String fileUpfileUrl = host + "/api/file/upload";
	public static String createWholeIdUrl = host + "/api/file/crewfid";
	public static String checkFileUrl = host + "/api/file/check";
	public static String createWholeIdByFielUrl = host + "/api/file/crewfidbwf";
	
	public static String testUrl = "http://192.168.70.178:5202/api/code/codeTableInfo/getTableNameList";
	
	public static String encode = "utf-8";
	public static String token = "D43D885B04344150B5B9C05F4C861AD3";
	
	
	public static void main(String[] args) throws Exception {
		
		int chunkSize = 30 * 1024 * 1024;
		
		String filePath = "D:\\newdown\\jdk1.8.0_20_i586.zip";
		filePath = "D:\\newdown\\tomcat5201.tar.gz";
		
//		fileUpload(filePath,"","","测试别名",token);
		
//		getWholeFileIdByParam(filePath,chunkSize,"jdk1.8.0_20_i586");
		
//		checkFileHash(DigestUtils.md5Hex(new FileInputStream(new File(filePath))),"","");
		
		testUrl(null);
		
		
//		JhkjFileUtil.splitFile(file, chunkSize ,"part");
		String [] upFiles = new String [] {
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000001.part", 
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000002.part",
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000003.part",
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000004.part",
				"D:\\newdown\\jdk1.8.0_20_i586.zip.000005.part"
			};
		// String [] upFiles = new String [] { "D:\\newdown\\test_doc.docx" };
		// UfdmFileUtil.combineFile(file,"part");
		
//		System.out.println("\n 开始获取wholeFileId:");
//		
//		String wholeFileId = getWholeFileIdByParam(filePath);
//		if(!wholeFileId.equals("-1")) {
//		    for(int i = 0; i < upFiles.length; i++) {
//				fileUpload(upFiles[i],wholeFileId,String.valueOf(i),"测试别名",token);
//		    }
//		}

	}
	
	/**
	 * Description: 通过参数（Hash 由客户端计算）获取 WholeFileId 
	 * @param fileHash 文件HASH
	 * @param wholeFileId 
	 * @param chunkNum 
	 * @return
	 * @throws Exception
	 * @author 
	 * @date 2019年10月25日
	 * @version 1.0
	 */
	public static String testUrl(Map<String,String> reqParamMap) throws Exception {
		Map<String,String> reqHeadMap = new HashMap<String,String>();
		reqHeadMap.put("Auth-User-Token", token);
		String testResult = CoreRequestHttpUtil.postParam(testUrl,reqHeadMap,reqParamMap,encode);
		System.out.println("\n testResult:"+testResult);
		return testResult;
	}
	
	/**
	 * Description: 通过参数（Hash 由客户端计算）获取 WholeFileId 
	 * @param fileHash 文件HASH
	 * @param wholeFileId 
	 * @param chunkNum 
	 * @return
	 * @throws Exception
	 * @author 
	 * @date 2019年10月25日
	 * @version 1.0
	 */
	public static boolean checkFileHash(String fileHash,String wholeFileId,String chunkNum) throws Exception {
		boolean exist  = false;
		Map<String,String> reqHeadMap = new HashMap<String,String>();
		reqHeadMap.put("Auth-User-Token", token);
		Map<String,String> reqParamMap = new HashMap<String,String>();
		reqParamMap.put("fileHash", fileHash);
		reqParamMap.put("wholeFileId", wholeFileId);
		reqParamMap.put("chunkNum", chunkNum);
		String getWholeFileIdResult = CoreRequestHttpUtil.postParam(checkFileUrl,reqHeadMap,reqParamMap,encode);
		JSONObject retResult = JSON.parseObject(getWholeFileIdResult);
		System.out.println(retResult);
		JSONObject retHead = (JSONObject) retResult.get("retHead");
		if(retHead.getString("code").equals("0000000")) {
			try {
			    exist = (boolean) retResult.get("retBody");
			}
			catch(Exception er) {
				
			}
		}
		System.out.println(wholeFileId);
		return exist;
	}
	
	/**
	 * Description: 通过参数（Hash 由客户端计算）获取 WholeFileId 
	 * @param filePath
	 * @return
	 * @throws Exception
	 * @author 
	 * @date 2019年10月25日
	 * @version 1.0
	 */
	public static String getWholeFileIdByParam(String filePath,long chunkSize,String aliases) throws Exception {
		File file = new File(filePath);
		String wholeFileId = "-1";
		Map<String,String> reqHeadMap = new HashMap<String,String>();
		reqHeadMap.put("Auth-User-Token", token);
		Map<String,String> reqParamMap = new HashMap<String,String>();
		reqParamMap.put("fileName", file.getName());
		reqParamMap.put("fileType", BaseStringUtil.getSuffixName(file.getName()));	
		reqParamMap.put("fileHash", DigestUtils.md5Hex(new FileInputStream(file)));
		reqParamMap.put("fileSize", String.valueOf(file.length()));
		reqParamMap.put("chunkSize", String.valueOf(chunkSize));
		reqParamMap.put("aliases", aliases);
		String getWholeFileIdResult = CoreRequestHttpUtil.postParam(createWholeIdUrl,reqHeadMap,reqParamMap,encode);
		JSONObject retResult = JSON.parseObject(getWholeFileIdResult);
		System.out.println(retResult);
		JSONObject retHead = (JSONObject) retResult.get("retHead");
		if(retHead.getString("code").equals("0000000")) {
			JSONObject retBody = (JSONObject) retResult.get("retBody");
			wholeFileId = retBody.getString("wholeFileId");
		}
		System.out.println(wholeFileId);
		return wholeFileId;
	}
	
	/**
	 * Description:  通过文件（Hash 由服务端计算）获取 WholeFileId 
	 * @param file
	 * @param aliases
	 * @param chunkSize
	 * @return
	 * @throws Exception
	 * @author 
	 * @date 2019年10月25日
	 * @version 1.0
	 */
	public static String getWholeFileIdByWholeFile(File file,String aliases,String chunkSize) throws Exception {
		String wholeFileId = "-1";
		Map<String,String> reqHeadMap = new HashMap<String,String>();
		reqHeadMap.put("Auth-User-Token", token);
		Map<String,String> reqParamMap = new HashMap<String,String>();
		reqParamMap.put("chunkSize", chunkSize);
		reqParamMap.put("aliases", aliases);
		Map<String, File> reqFileMap = new HashMap<String, File>();
		reqFileMap.put("file", file);
		String getWholeFileIdResult = CoreRequestHttpUtil.postFile(createWholeIdByFielUrl, reqHeadMap, reqParamMap, reqFileMap, encode);
		JSONObject retResult = JSON.parseObject(getWholeFileIdResult);
		System.out.println(retResult);
		JSONObject retHead = (JSONObject) retResult.get("retHead");
		if(retHead.getString("code").equals("0000000")) {
			JSONObject retBody = (JSONObject) retResult.get("retBody");
			wholeFileId = retBody.getString("id");
		}
		System.out.println(wholeFileId);
		return wholeFileId;
	}
	
	/**
	 * Description: 单文件上传
	 * @param filePath      文件路径
	 * @param wholeFileId   大文件 wholeFileId
	 * @param chunkNum      当前块编号
	 * @param aliases       别名
	 * @param token         
	 * @throws Exception
	 * @author 
	 * @date 2019年10月24日
	 * @version 1.0
	 */
	public static void fileUpload(String filePath,String wholeFileId,String chunkNum,String aliases,String token) throws Exception {
		
		Map<String,String> reqHeadMap = new HashMap<String,String>();
		reqHeadMap.put("Auth-User-Token", token);
		
		Map<String,String> reqParamMap = new HashMap<String,String>();
		reqParamMap.put("wholeFileId", wholeFileId);
		reqParamMap.put("chunkNum", chunkNum);
		reqParamMap.put("aliases", aliases);
		
		Map<String, File> reqFileMap = new HashMap<String, File>();
		reqFileMap.put("file", new File(filePath));
		System.out.println(CoreRequestHttpUtil.postFile(fileUpfileUrl,reqHeadMap,reqParamMap,reqFileMap,encode));
		
		/**
		// 使用 MultipartFile 格式进行文件上传
		Map<String, MultipartFile> reqMultipartFileMap = new HashMap<String, MultipartFile>();
        FileInputStream fis = new FileInputStream(new File(filePath));
        // 通过 FileInputStream 上传
        reqMultipartFileMap.put("file", JhkjRequestHttpUtilMultipartFile.getMulFileByStream("test",fis));
        // 通过 文件路径 上传
		reqMultipartFileMap.put("file", JhkjRequestHttpUtilMultipartFile.getMulFileByPath(filePath));
		System.out.println(JhkjRequestHttpUtilMultipartFile.postMultipartFile(fileUpfileUrl,reqHeadMap,reqParamMap,reqMultipartFileMap,encode));
		*/
	}
	
}
